package 多线程.线程通信;

import java.util.ArrayList;
import java.util.List;

public class 桌子 {
    public List<String> 包子 = new ArrayList<>();

    public synchronized void 放包子() {
        try {
            if (包子.isEmpty()) {
                包子.add(Thread.currentThread().getName() + "做的包子");
                System.out.println(Thread.currentThread().getName() + "做了一个包子");
                Thread.sleep(1000);
                this.notify();
                this.wait();
            } else {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public synchronized void 吃包子() {
        try {
            String 吃货 = Thread.currentThread().getName();
            if (包子.size() == 1) {
                System.out.println(吃货 + "吃了" + 包子.get(0));
                包子.remove(0);
                Thread.sleep(1000);
                this.notifyAll();
                this.wait();
            }
             else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
